package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * SetRequestNormal类
 * 实现设置请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class SetRequestNormal implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid = null;
    private OAD oad = null;
    private Data data = null;

    /**
     * 默认构造函数
     */
    public SetRequestNormal() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置所有参数
     * 
     * @param piid PIID
     * @param oad  对象属性描述符
     * @param data 数据
     */
    public void setAll(PIID piid, OAD oad, Data data) {
        this.piid = piid;
        this.data = data;
        this.oad = oad;
    }

    /**
     * 设置对象属性描述符
     * 
     * @param oad 对象属性描述符
     */
    public void setOAD(OAD oad) {
        this.oad = oad;
    }

    /**
     * 设置PIID
     * 
     * @param piid PIID
     */
    public void setPIID(PIID piid) {
        this.piid = piid;
    }

    /**
     * 设置数据
     * 
     * @param data 数据
     */
    public void setData(Data data) {
        this.data = data;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.data = new Data();
        codeLength += this.data.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.data != null && this.oad != null && this.piid != null) {
                codeLength = 0;
                codeLength += this.data.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("oad or piid or data is null");
            }
        }

        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding SetRequestNormal", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piid != null && this.oad != null && this.data != null) {
            return "sequence: {" + "piid: " + this.piid + ", oad: " + this.oad + ", data: " + this.data + "}";
        } else {
            return "piid || oad || data is null";
        }
    }
}